<template>
	<div class="send_email col-sm-12" :style="{height: heightData}">
		<div class="signform sign_limit_width">
			<img src="../assets/img/logo.png" alt="" class="sign_logo cursor" @click="goHome()"/>
			<h2>发送邮件</h2>
			<p class="someInfo">请输入登录邮箱</p>
			<div class="text-left">
				<label for="email">电子邮件</label>
				<input type="eamil" class="form-control" placeholder="邮箱" v-model="sendEmailVal" @blur="checkMail()" v-bind:class="sendClassTip0"/></br>
				<p class="reg_tips" v-if="seenEmail">{{tipMsg1}}</p>
			</div>
			<p class="log_tips" v-show="notVerification">你的邮箱还未验证
				<a href="javascript:;" @click="resendEmail()" class="link_to">发送验证链接</a>
			</p>
			<button class="btn btn_sign" @click="sendEmail()">发送忘记密码链接</button> 
			<p class="footer_company">© 2016-2018 北京晶派科技有限公司</p>
		</div>
	</div>
</template>

<script>
	import {urlSendEmail,urlResendEmail} from '../api/api';
	export default {
		name: 'sendEmail',
		data () {
		    return {
		    	sendClassTip0:{
		    		'form-control':true,
			        'tip': false
		    	},
                sendEmailVal: '',
                heightData:'',
                notVerification:false,
                seenEmail:false,
                tipMsg1:''
		    }
		},	
		mounted(){
	    	  this.heightData=`${document.documentElement.clientHeight}px`
	    },
		methods: {	
			goHome(){
				this.$router.replace('/');
			},
			checkMail:function() {
	  			let filter= /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
				if (! filter.test(this.sendEmailVal)){
					this.sendClassTip0.tip=true;
					this.seenEmail=true;
					this.tipMsg1='抱歉，邮箱格式不正确';
				 	$(".btn_sign").attr("disabled",true)
				}else{
					this.seenEmail=false;
				 	this.sendClassTip0.tip=false;
				 	$(".btn_sign").attr("disabled",false)
				}
			},
			resendEmail(){
				//0: 重发验证邮件,1: 重发重置邮件
				let sendData={
					email:this.sendEmailVal,
					type:0
				};
				urlResendEmail(sendData).then(response => {
					if(response.data.code==200){
						this.$router.push({name:'resendEmail',params: {email:this.sendEmailVal}})	
					}else{
						this.seenLogin=true;
						this.logTip2=response.data.message;
					}
        		})				
			},
	       	sendEmail(){
	       		if(this.sendEmailVal =='' ){
	       			this.seenEmail=true;
	       			this.tipMsg1='请输入邮箱地址';
					return false;
				}else{
					let sendData={
						email:this.sendEmailVal
					}
	        		//重置密码请求
	         		urlSendEmail(sendData).then(response => {
		              	if(response.data.code==200){	              		
		              		let username=response.data.data.username,
				            	token=response.data.data.token;
				            localStorage.setItem('res_token',token); 
					    	localStorage.setItem('username',username); 
					    	this.$router.push({name:'resendEmail',params: {email:this.sendEmailVal}})	  
		              	}else if(response.data.code==10006){
		              		this.notVerification=true;
		              	}else if(response.data.code==201){
		              		this.tipMsg1='抱歉，邮件发送失败，请重新尝试！';
		              		this.seenEmail=true;
		              		console.log('email请求error')
		              	}else{
		              		localStorage.clear();
		              	}
		            })
				}
	       		
	       	}
		}
	}
</script>

<style>
	@import "../assets/css/sign.css"
</style>